import 'package:get/get.dart';
import 'package:sgy_flutter/extensions/get.dart';

class DeviceSettingLogic extends GetxController {
  var loading = true.obs;
  Rxn<int> leftSensorType = Rxn(0);
  Rxn<int> rightSensorType = Rxn(0);
  var showRight = false.obs;
  var showLeft = false.obs;
  @override
  void onReady() async {
    if (bleManager.isReady.value == false &&
        rightBleManager?.isReady.value == false) {
      await bleManager.ensureDeviceConnected();
    }
    if (bleManager.isReady.value == true ||
        rightBleManager?.isReady.value == true) {
      try {
        final data = await bleManager.getSensorType();
        leftSensorType(data);
        showLeft(true);
      } catch (e) {}
      try {
        final data = await rightBleManager?.getSensorType();
        rightSensorType(data);
        showRight(true);
      } catch (e) {}
    }
    loading(false);
    super.onReady();
  }

  Future<bool> changeLeftSensorType(int type)async{
    int changeType = leftSensorType.value == 1 ? 2 : 1;
    if (await bleManager.ensureDeviceConnected()){
      await bleManager.setSensorType(changeType);
      leftSensorType(changeType);
    }
    return leftSensorType.value == type;
  }

  Future<bool> changeRightSensorType(int type)async{
    int changeType = rightSensorType.value == 1 ? 2 : 1;
    if (await rightBleManager?.ensureDeviceConnected() == true){
      await rightBleManager?.setSensorType(changeType);
      rightSensorType(changeType);
    }
    return rightSensorType.value == type;
  }

}
